--[[
商城界面
2016_06_28 Ravioyla

包含 JunFuTongPay ShopPay ShopLayer 三个类
]]


local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local MultiPlatform = appdf.req(appdf.EXTERNAL_SRC .. "MultiPlatform")
local targetPlatform = cc.Application:getInstance():getTargetPlatform()
local KeFuLayer = appdf.req(appdf.CLIENT_SRC.."plaza.views.layer.plaza.KeFuLayer")
local Utils = appdf.req(appdf.EXTERNAL_SRC .. "Utils")
local QueryExit = appdf.req(appdf.CLIENT_SRC.."app.views.layer.other.QueryDialog")

--支付选择页面
local ShopPay = class("ShopPay", cc.Layer)


local BT_CLOSE = 201
local BT_SURE = 202
local BT_CANCEL = 203



function ShopPay:ctor(parent,scene,cfg)

	-- AudioEngine.playEffect("sound/charge.wav")


	self.m_parent = parent
	--价格
	self.m_fPrice = 0.00
	--数量
	self.m_nCount = 0
	-- appid
	self.m_nAppId = 0
	self.scene = scene
	self.othermax = 0
	
	self:showPopWait()
	--加载csb资源
	local rootLayer, csbNode
	self.priceList = {3,8,18,60,88,98}
	
	rootLayer, csbNode = ExternalFun.loadRootCSB("Shop/ShopPayLayer.csb", self)
	self.csbNode = csbNode
	local cury = self.csbNode:getPositionY()
	self.csbNode:setPositionY((display.height-750)/2)
	self.m_spBgKuang = csbNode:getChildByName("shop_pay_bg")
	-- ExternalFun.FixTopUI(csbNode)
	--	self:setScale(0.0001)
	self.agentPanel = csbNode:getChildByName("agentPanel")
	self.payPanel = csbNode:getChildByName("payPanel")
	self.agentDemo = csbNode:getChildByName("agentDemo")

	ExternalFun.FixScaleUI(self.payPanel)
	ExternalFun.FixScaleUI(self.agentPanel)

	local nodesize = csbNode:getContentSize()
	if nodesize.height > display.height then
		local scale = display.height/nodesize.height
		-- self.agentPanel:setAnchorPoint(cc.p(0,1))
		-- self.payPanel:setAnchorPoint(cc.p(0,1))
		-- self.agentDemo:setAnchorPoint(cc.p(0,1))
		-- self.agentPanel:setScale(scale)
		-- self.payPanel:setScale(scale)
		-- self.agentDemo:setScale(scale)
	end

	--csbNode:getChildByName("userGold_text"):setString(ExternalFun.ChangeScore(GlobalUserItem.lUserScore,2,","))
	local  clearBt = self.payPanel:getChildByName("clearBt")
	clearBt:addTouchEventListener(function (ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			self.edit_Money:setText("")
		end
	end )
	
	local tempeditbox = clearBt:getChildByName("temp_Edixbox")
	self.edit_Money = ccui.EditBox:create(tempeditbox:getContentSize(),ccui.Scale9Sprite:create(""))
	:setPosition(tempeditbox:getPosition())
	:setFontName("fonts/round_body.ttf")
	:setPlaceholderFontName("fonts/round_body.ttf")
	:setFontSize(tempeditbox:getFontSize())
	:setMaxLength(16)
	:setInputMode(cc.EDITBOX_INPUT_MODE_NUMERIC)
	:setPlaceHolder("请输入充值金额")
	:addTo(tempeditbox:getParent())
	tempeditbox:removeFromParent()
	
	
	
	local  czbt = self.payPanel:getChildByName("czbt") --充值按钮
	czbt:addTouchEventListener(function (ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			self:topay()
			ref:setScale(1)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end )
	local bg = self.m_spBgKuang
	--按钮回调
	local btcallback = function (ref, type)
		
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			self:onButtonClickedEvent(ref:getTag(),ref)
			ref:setScale(1)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end
	self.btcallback = btcallback
	--关闭按钮
	local btn = bg:getChildByName("btn_close")
	btn:setTag(BT_CLOSE)
	btn:addTouchEventListener(btcallback)
	
	--加载动画
	self.m_actShowAct = cc.ScaleTo:create(0.2, 1.0)
	ExternalFun.SAFE_RETAIN(self.m_actShowAct)
	local scale = cc.ScaleTo:create(0.2, 0.0001)
	local call = cc.CallFunc:create(function ( )
		self:removeFromParent()
	end)
	self.m_actHideAct = cc.Sequence:create(scale, call)
	ExternalFun.SAFE_RETAIN(self.m_actHideAct)
	local rmbNumTabel = {10,50,100,500,1000,2000}
	self.PaybtnlistView = self.m_spBgKuang:getChildByName("PaybtnlistView")
	self.PaybtnlistView:setScrollBarEnabled(false)
	self.agentListView = self.agentPanel:getChildByName("agentListView")
	self.agentPanel:getChildByName("myAcount"):setString(GlobalUserItem.dwUserID)
	self.agentPanel:getChildByName("jbyj_btn") :addTouchEventListener(function (ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			self:OpenReportLayer()
			ref:setScale(1)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end )
	local Effectsbtn =  self.agentPanel:getChildByName("jbyj_btn")
	Utils.lightEffects(Effectsbtn,display.newSprite("Shop/jbLight.png"),display.newSprite("Shop/jbyj1.png"))
	local payjson = {}
	payjson.userid = GlobalUserItem.dwUserID
	payjson.channelid = appdf.channelId
	self.agentPanel:getChildByName("refrash_btn") :addTouchEventListener(function (ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			ref:setScale(1)
			self:showPopWait()
			 Utils.getPayInfo( payjson,handler(self,self.refreshAgent) )
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end )
	self:initContactPanel()
	self.payPanel:setVisible(false)
	local  copyidbt = self.agentPanel:getChildByName("copyidbt") --充值按钮
	copyidbt:addTouchEventListener(function (ref, type1)
		if type1 == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
		local ret, msg = MultiPlatform:copyToClipboard(GlobalUserItem.dwUserID .. "")
		if ret == true then
			showToast(self, "复制成功!", 1)
		else
		if type(msg) == "string" then
			showToast(self, msg, 1, cc.c3b(250, 0, 0))
		end
	end
		end
	end )
	
	local function infocb(code,msg)
		if msg then
			showToast(self,msg,1)
		end
		if code == 0  then
		end
	end

	 Utils.getPayInfo( payjson,handler(self,self.initPayChannel) )
	
end
---打开举报按钮
function ShopPay:OpenReportLayer()
	if self.reportLayer then
		ExternalFun.playOpenLayerAnimation( self.bg_panel )
		self.reportLayer:setVisible(true)
	else
		self.reportLayer = ExternalFun.loadCSB( "Shop/ReportLayer.csb", self )
		local bg_panel = self.reportLayer:getChildByName("Panel_1")
		self.bg_panel = bg_panel
		ExternalFun.playOpenLayerAnimation( bg_panel )
		bg_panel:getChildByName("closeBt"):addTouchEventListener(function (ref,type)
			if type == ccui.TouchEventType.ended then
				ExternalFun.playClickEffect( )
				self.reportLayer:setVisible(false)
				ref:setScale(1)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
			end
		end)

		--举报按钮
		local jbbtn = bg_panel:getChildByName("jubaobt")
		
		jbbtn:addTouchEventListener(function (ref,Touchtype)
			if Touchtype == ccui.TouchEventType.ended then			
				self.scene:onChangeShowMode(yl.SCENE_KEFU)
				local _KeFuLayLayer = self.scene._sceneLayer:getChildByTag(yl.SCENE_KEFU)
				if _KeFuLayLayer then
					_KeFuLayLayer:jumptoChatview()
				end		
				ref:setScale(1)
			elseif Touchtype == ccui.TouchEventType.began then
				ref:setScale(yl.btscale)
				return true
			elseif Touchtype == ccui.TouchEventType.canceled then  
				ref:setScale(1)
			end
		end)
		
	end
end
function ShopPay:initContactPanel()
	self.contactPanel = self.agentPanel:getChildByName("contactPanel")
	self.contactPanel:setVisible(false)
	
	--复制WX号
	local copyWX_btn = self.contactPanel:getChildByName("copywx_btn")
	copyWX_btn:addTouchEventListener(function (ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			local weixin = copyWX_btn:getChildByName("wx_text"):getString()

			local ret, msg = MultiPlatform:copyToClipboard(weixin)
			if ret == true then
				showToast(self, "复制成功!", 1)
			else
				showToast(self, msg or "复制失败", 1, cc.c3b(250, 0, 0))
			end
			Utils.checkAPPInstalled("wxpay")
			cc.Application:getInstance():openURL("weixin://")
			ref:setScale(1)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end)
	--复制QQ号
	local copyQQ_btn = self.contactPanel:getChildByName("copyqq_btn")
	copyQQ_btn:addTouchEventListener(function (ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
				local qq = copyQQ_btn:getChildByName("QQ_text"):getString()
				local ret, msg = MultiPlatform:copyToClipboard(qq)
				if ret == true then
					showToast(self, "复制成功!", 1)
				else
					showToast(self, msg  or "复制失败", 1, cc.c3b(250, 0, 0))
				end
				Utils.checkAPPInstalled("qqpay")
				cc.Application:getInstance():openURL("mqq://im/chat?chat_type=wpa")
				ref:setScale(1)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end)
	local qqpx,qqpy = copyQQ_btn:getPosition()
	local wxpx,wxpy = copyWX_btn:getPosition()
	self.contactPanel:getChildByName("close_btn"):addTouchEventListener(function (ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			self.contactPanel:setVisible(false)
			self.copyQQ_btn:setPosition(qqpx,qqpy)
			self.copyWX_btn:setPosition(wxpx,wxpy)
			ref:setScale(1)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end)
	self.copyQQ_btn = copyQQ_btn
	self.copyWX_btn = copyWX_btn
end
function ShopPay:initPayChannel(recdata)
	if tolua.isnull(self.agentPanel) then
		return
	end
	self.agentPanel:setVisible(true)
	self.payPanel:setVisible(false)
	self.payData = recdata
	self:showPayinfo(self.payData)
		self:dismissPopWait()
end



function ShopPay:showPayinfo(payinfoDate)
	dump(payinfoDate,"payinfoDate")
	if not payinfoDate  then
		showToast(self.scene,"获取数据失败,请重试",2)
		return
	end
	if not payinfoDate.policy  then
		showToast(self.scene,"策略获取失败,请重试",2)
		return
	end
	local OnlyShowAgentRate = 0 --只展示代理的概率
	local showThirdPay = true   -- 展示官方支付  f
	self.showThirdPay = showThirdPay
	

	local policy = payinfoDate.policy or {}
	cc.UserDefault:getInstance():setIntegerForKey("hasRechargeCount",ExternalFun.ChangeScore(policy.paycount or 0,2,","))
	self.scene:RefreshmoveBottomBtn()
	if policy.bopen then
		local strategy = policy.strategy
	
	local paytime = policy.paytime  --玩家充值次数
	local paycount = policy.paycount --玩家充值金额

	local agentpaytime = policy.agentpaytime -- 代理充值次数
	if self.scene.lastPayTime ~= paytime then
		self.scene.ShowAgentTime = 1
	end
	
	self.scene.lastPayTime = paytime
	
	
	local AgentRechargeRate =  strategy.AgentRechargeRate --代理充值过条件下  只展示代理充值的概率
	local AgentRechargeRateInc =  strategy.AgentRechargeRateInc  --代理充值过条件下 每次成功代理充值 增加的概率
	local AgentShowNum = strategy.AgentShowNum           --代理充值过且连续展示代理次数后还未代理充值超过的次数
	local AgentNextRate = strategy.AgentNextRate         --代理充值过且连续展示代理次数后还未代理充值后降低的概率

	local RechargeNum = strategy.RechargeNum             --未代理充值过条件下 充值成功次数
	local RechargeGold = strategy.RechargeGold           --未代理充值过条件下 充值金额
	local AgentRate = strategy.AgentRate                 --未代理充值过且达到充值次数和金额的条件下 只展示代理的概率

	local ShowNum = strategy.ShowNum                     --未代理充值且连续展示代理次数后还未代理充值超过的次数
	local NextRate = strategy.NextRate                   --未代理充值且连续展示代理次数后还未代理充值后降低的概率
	math.randomseed(os.time())
	local r = math.random()
	if agentpaytime >= 1 then
		self.othermax = strategy.OtherMax
		OnlyShowAgentRate = AgentRechargeRate +  AgentRechargeRateInc*(agentpaytime-1) 
		local ss = self.scene.ShowAgentTime 
		if self.scene.ShowAgentTime > AgentShowNum then
			OnlyShowAgentRate = OnlyShowAgentRate - AgentNextRate*(self.scene.ShowAgentTime - AgentShowNum)
			OnlyShowAgentRate = OnlyShowAgentRate <= 0  and 0 or OnlyShowAgentRate
		end
		OnlyShowAgentRate = OnlyShowAgentRate/100
	elseif paytime > RechargeNum  or paycount > RechargeGold then
		OnlyShowAgentRate =  AgentRate
		if self.scene.ShowAgentTime > ShowNum then
			OnlyShowAgentRate = OnlyShowAgentRate - NextRate*(self.scene.ShowAgentTime - ShowNum)
			OnlyShowAgentRate = OnlyShowAgentRate <= 0  and 0 or OnlyShowAgentRate
		end
		OnlyShowAgentRate = OnlyShowAgentRate/100
	end
	showThirdPay = r > OnlyShowAgentRate
	
--	if self.scene.ShowAgentTime > 
	if not showThirdPay then
		self.scene.ShowAgentTime = self.scene.ShowAgentTime + 1
	end
	
	end
	
	
	
	local btCount =0
	self.tbAllBtn ={}
	self.PaybtnlistView:removeAllItems()
	for k,v in pairs(payinfoDate) do
		if  k ~= "policy" then
		   if showThirdPay or k == "agentpay"then
			local paybt = ccui.Button:create("Shop/"..k.."_2.png","Shop/"..k.."_2.png","Shop/"..k.."_1.png")
			paybt:setName(k)
			paybt:addTouchEventListener(self.btcallback)
			display.newSprite("public/btnline.png"):move(134,5):addTo(paybt)
			display.newSprite("public/btnline.png"):move(134,94):addTo(paybt)
			if paybt then
			  if cc.FileUtils:getInstance():isFileExist("Shop/"..k.."_2.png") then
				 self.tbAllBtn[v.order_id] = paybt
				 if v.order_id > btCount then
					 btCount = v.order_id
				 end
				
			  end
			
		    end
		   end
		  
		end
		
	end
	--------------------------
	 for k,v in pairs(self.tbAllBtn)  do
	 	if not tolua.isnull(v) then
	 		self.PaybtnlistView:pushBackCustomItem(v)
	 	end
	 end
	local btIdx = 1
	while true do
		
		if btIdx > btCount then
			showToast(self,"充值系统维护中",1)
			break
		end
		if self.tbAllBtn[btIdx] then
			self.disableBt = self.tbAllBtn[btIdx]
			self:onButtonClickedEvent(nil,self.tbAllBtn[btIdx])
			break
		else
			btIdx = btIdx+1
			
		end
	end
	
end
function ShopPay:refreshAgent(payinfoDate)
	
	self.payData = payinfoDate
	self:ShowAgentPanel()
		self:dismissPopWait()
end
function ShopPay:isPayMethodValid()
	if (yl.WeChat.PartnerID ~= " " and yl.WeChat.PartnerID ~= "")
		or (yl.AliPay.PartnerID ~= " " and yl.AliPay.PartnerID ~= "")
		or (yl.JFT.PartnerID ~= " " and yl.JFT.PartnerID ~= "")
		then
		return true
	else
		return false
	end
end

function ShopPay:showLayer(var)
	self:setVisible(var)
	
	if true == var then
		self:stopAllActions()
		self:runAction(self.m_actShowAct)
	else
		--    self:hide()
		--        self.m_spBgKuang:stopAllActions()
		--		self.m_spBgKuang:runAction(self.m_actHideAct)
	end
end

function ShopPay:ShowAgentPanel()
	self.agentListView:removeAllItems()
	self.isInitAgent = true
	
	
	local agentData =  self.payData["agentpay"]["item_value"]
	local lie = math.ceil(#agentData/2)
	local han = 4
	--local j = 3
	for i = 1,lie do
		local itemlistView = self.agentDemo:clone()
		itemlistView:setScrollBarEnabled(false)
		for j =1,han do
			local item = itemlistView:getItem((j-1))
			if agentData[(i-1)*han+j] and ((i-1)*han+j)<=8 then
				local itemData = agentData[(i-1)*han+j]
				local pay_btn = item:getChildByName("pay_btn")
				local agentName_text = item:getChildByName("agentName_text")
				
				agentName_text:setString(itemData["nickname"])
				pay_btn:addTouchEventListener(function (ref, type)
					if type == ccui.TouchEventType.ended then
						ExternalFun.playClickEffect( )
						self.contactPanel:setVisible(true)
						self.contactPanel:getChildByName("agentName_text"):setString(itemData["nickname"])
						if itemData["qq"] and itemData["qq"] ~= "" then
							self.copyQQ_btn:getChildByName("QQ_text"):setString(itemData["qq"])
							self.copyQQ_btn:setVisible(true)
						else
							self.copyQQ_btn:setVisible(false)
							
						end
						
						if itemData["wechat"] and itemData["wechat"] ~= "" then
							self.copyWX_btn:getChildByName("wx_text"):setString(itemData["wechat"])
								self.copyWX_btn:setVisible(true)
						else
							self.copyWX_btn:setVisible(false)
						end
						if not self.copyQQ_btn:isVisible() and self.copyWX_btn:isVisible() then
							self.copyWX_btn:setPositionY(379)
						elseif self.copyQQ_btn:isVisible() and not self.copyWX_btn:isVisible()  then
							self.copyQQ_btn:setPositionY(379)
						end
						ref:setScale(1)
					elseif type == ccui.TouchEventType.began then
						ref:setScale(yl.btscale)
						return true
					elseif type == ccui.TouchEventType.canceled then  
						ref:setScale(1)
					end
				end)
				item:setVisible(true)
				
			else
				item:setVisible(false)
			end
		end
		
		self.agentListView:pushBackCustomItem(itemlistView)
		
	end
end
function ShopPay:showPayChannel(payname)
	local switch = {
	["qqpay"] = function ()
		return self.payData["qqpay"]["item_value"],"温馨提示：1.充值比例1元=1金币。 2.需要安装QQ"
	end,
	["unionpay"] = function ()
		return self.payData["unionpay"]["item_value"],"温馨提示：1.充值比例1元=1金币。 2.需要开通网银的银联卡" 
	end,
	["wxpay"] = function ()
		return self.payData["wxpay"]["item_value"],"温馨提示：1.充值比例1元=1金币。 2.需要安装微信"
	end,
	["alipay"] = function ()
		return self.payData["alipay"]["item_value"],"温馨提示：1.充值比例1元=1金币。 2.需要安装支付宝"
	end,
	["alipayfixed"] = function ()
		return self.payData["alipayfixed"]["item_value"],"温馨提示：1.充值比例1元=1金币。 2.需要安装支付宝"
	end,
	["jdpay"] = function ()
		return self.payData["jdpay"]["item_value"],""
	end
	}
	self.edit_Money:setText("")
	local paydata,hintStr = switch[payname]()
	self.minPay = 10   --最小充值数
	self.maxPay = 100   --最大充值数
	local hinttext = self.payPanel:getChildByName("hintText")
	hinttext:setString(hintStr)
	local flagiconsp = hinttext:getChildByName("flagiconsp")
	local flagnamesp = hinttext:getChildByName("flagnamesp")
	local subbg = self.payPanel:getChildByName("subbg")
	flagiconsp:setTexture("Shop/flag_"..payname..".png")
	flagnamesp:setTexture("Shop/flagname_"..payname..".png")
	subbg:setTexture("Shop/subbg_"..payname..".png")
	local isfix = tonumber( self.payData[payname].fixedgold )
	if isfix == 1 then

		self.edit_Money:setPlaceHolder("请选择充值金额")
		self.edit_Money:setTouchEnabled(false)
	else
		self.edit_Money:setPlaceHolder("请输入充值金额")
		self.edit_Money:setTouchEnabled(true)
	end
	local jumpovercount = 1
	local j = 0
	for i= 1,8 do
		--每个充值按钮
		
		local paybtitem = self.payPanel:getChildByName("pay"..jumpovercount)
		if paydata and paydata[i] and  paydata[i] ~= 0 then
			print("paydata[i]",i,jumpovercount)
			jumpovercount = jumpovercount+1
			paybtitem:setVisible(true)
			
			paybtitem:getChildByName("gold_text"):setString(paydata[i])
			paybtitem:addTouchEventListener(function (ref, type1)
				if type1 == ccui.TouchEventType.ended then
					ExternalFun.playClickEffect( )
					local curpay 
					if isfix ~= 1 then
						 curpay = tonumber(self.edit_Money:getText()) or 0
					else
						curpay = 0
					end
					self.edit_Money:setText(paydata[i]+curpay)
					ref:setScale(1)
				elseif type == ccui.TouchEventType.began then
					ref:setScale(yl.btscale)
					return true
				elseif type == ccui.TouchEventType.canceled then  
					ref:setScale(1)
				end
			end)
		
		else
			j = j+1
		end
		
		
	end
	for i=1,j do
		local paybtitem = self.payPanel:getChildByName("pay"..(9-i))
		paybtitem:setVisible(false)
	end
	self.minPay  = tonumber(self.payData[payname].min) 
	self.maxPay = tonumber(self.payData[payname].max)
	if self.othermax ~= 0 and payname ~="alipay" then
		self.maxPay = self.othermax
	end
	
end
function ShopPay:onButtonClickedEvent(tag, sender)
	self.selctType = sender:getName()
--	ExternalFun.playClickEffect( )
	if tag == BT_CLOSE or tag == BT_CANCEL then
		--    :addTouchEventListener(function(ref, type)
		--	 	if type == ccui.TouchEventType.ended then
		local gamelayer = self.scene._sceneLayer:getChildByTag(yl.SCENE_GAME) 
		if gamelayer then
			self.m_parent:removeFromParent()
		else
		self.scene:onKeyBack()
		end
		
		--		end
		--	end)
		--self:hide()
	elseif sender:getName() == "agentpay" then
		self.disableBt:setEnabled(true)
		self.disableBt = sender
		sender:setEnabled(false)
		self.agentPanel:setVisible(true)
		self.payPanel:setVisible(false)
		-- if not self.isInitAgent then
		self:ShowAgentPanel()
		-- end
	elseif sender:getName() == "qqpay" then
		self.disableBt:setEnabled(true)
		self.disableBt = sender
		sender:setEnabled(false)
		self.agentPanel:setVisible(false)
		self.payPanel:setVisible(true)
		self:showPayChannel("qqpay")
	elseif sender:getName() == "unionpay" then
		self.disableBt:setEnabled(true)
		self.disableBt = sender
		sender:setEnabled(false)
		self.agentPanel:setVisible(false)
		self.payPanel:setVisible(true)
		self:showPayChannel("unionpay")
	elseif sender:getName() == "wxpay" then
		self.disableBt:setEnabled(true)
		self.disableBt = sender
		sender:setEnabled(false)
		self.agentPanel:setVisible(false)
		self.payPanel:setVisible(true)
		self:showPayChannel("wxpay")
	elseif sender:getName() == "alipay" then
		self.disableBt:setEnabled(true)
		self.disableBt = sender
		sender:setEnabled(false)
		self.agentPanel:setVisible(false)
		self.payPanel:setVisible(true)
		self:showPayChannel("alipay")
	elseif sender:getName() == "alipayfixed" then
		self.disableBt:setEnabled(true)
		self.disableBt = sender
		sender:setEnabled(false)
		self.agentPanel:setVisible(false)
		self.payPanel:setVisible(true)
		self:showPayChannel("alipayfixed")	
	elseif sender:getName() == "jdpay" then
		self.disableBt:setEnabled(true)
		self.disableBt = sender
		sender:setEnabled(false)
		self.agentPanel:setVisible(false)
		self.payPanel:setVisible(true)
		self:showPayChannel("jdpay")
	end
end

function ShopPay:onTouchBegan(touch, event)
	return self:isVisible()
end

function ShopPay:onTouchEnded(touch, event)
	local pos = touch:getLocation();
	local m_spBg = self.m_spBgKuang
	pos = m_spBg:convertToNodeSpace(pos)
	local rec = cc.rect(0, 0, m_spBg:getContentSize().width, m_spBg:getContentSize().height)
	if false == cc.rectContainsPoint(rec, pos) then
		self:hide()
	end
end

function ShopPay:onExit()
	ExternalFun.SAFE_RELEASE(self.m_actShowAct)
	self.m_actShowAct = nil
	ExternalFun.SAFE_RELEASE(self.m_actHideAct)
	self.m_actHideAct = nil
end


--商城页面
local ShopLayer = class("ShopLayer", function (scene)
	local shopLayer = display.newLayer(cc.c4b(0, 0, 0, 125))
	return shopLayer
end)

ShopLayer.CBT_SCORE			= 1
ShopLayer.CBT_BEAN			= 2
ShopLayer.CBT_VIP			= 3
ShopLayer.CBT_PROPERTY		= 4
ShopLayer.CBT_ENTITY		= 5

ShopLayer.BT_SCORE			= 30
ShopLayer.BT_VIP			= 50
ShopLayer.BT_PROPERTY		= 60
ShopLayer.BT_GOODS			= 120
ShopLayer.BT_BEAN			= 520

ShopLayer.BT_ORDERRECORD    = 1001
ShopLayer.BT_BAG            = 1002

local SHOP_BUY = {}
SHOP_BUY[ShopLayer.BT_SCORE] = "shop_score_buy"
SHOP_BUY[ShopLayer.BT_BEAN] = "shop_bean_buy"
SHOP_BUY[ShopLayer.BT_VIP] = "shop_vip_buy"
SHOP_BUY[ShopLayer.BT_PROPERTY] = "shop_prop_buy"
SHOP_BUY[ShopLayer.BT_GOODS] = "shop_goods_buy"

-- 支付模式
local APPSTOREPAY = 10 -- iap支付
local THIRDPAY = 20 -- 第三方支付

-- 进入场景而且过渡动画结束时候触发。
function ShopLayer:onEnterTransitionFinish()
	
end

-- 退出场景而且开始过渡动画时候触发。
function ShopLayer:onExitTransitionStart()
	return self
end

--scene
--stmod 进入商店后的选择类型
function ShopLayer:ctor(scene, stmod,cfg)
	self.scene = scene
	
	if nil == self.m_payLayer then
		self.m_payLayer = ShopPay:create(self,scene,cfg)
		self:addChild(self.m_payLayer)
	end
	
	self.m_payLayer:showLayer(true)
end



function ShopLayer:onKeyBack()
	if nil ~= self.m_payLayer then
		if true == self.m_payLayer:isVisible() then
			return true
		end
		
		if true == self.m_bJunfuTongPay then
			return true
		end
	end
	return false
end

function ShopPay:showPopWait()
	self.scene:showPopWait()
	-- performWithDelay(self,function() self:dismissPopWait() end,3)
end

function ShopPay:dismissPopWait()
	self.scene:dismissPopWait()
end

function ShopPay:topay()
	
	if not Utils.checkAPPInstalled(self.selctType)  then
		if  device.platform ~= "windows" then
			return
		end
	end
	
	function cb( code , msg )
		print("ShopPay:toplay>>>>>>>>>>>",code)
		if code ~= 0 then
			local curScene = cc.Director:getInstance():getRunningScene()
			QueryExit:create(msg or "获取订单失败", function (ok)
				if ok == true then
				end
			end)
			:addTo(curScene)
			-- showToast(self,msg or "获取订单失败",2)
			return
		end
	end
	

	local curpay = tonumber(self.edit_Money:getText()) or 0
	if self.minPay then 
		if curpay < self.minPay then
			showToast(self,"充值金额不能少于"..self.minPay,2)
			return
		elseif curpay > self.maxPay then
			showToast(self,"充值金额不能大于"..self.maxPay,2)
			return
		end
	end
	local payTb = {}
	payTb.paymethod = self.selctType	--	支付方式 取值为 alipay wxpay unionpay qqpay
	payTb.money = curpay*100
	payTb.userid = GlobalUserItem.dwUserID
	payTb.channelid = GlobalUserItem.channelId
	-- payTb.clientip = "192.168.188.131"
	self:showPopWait()
	Utils.topay(payTb,self.selctType,cb,self)
end

return ShopLayer